import Vue from 'vue'
import axios from 'axios'

let iconv = require('iconv-lite')

const baseUrl = process.env.baseUrl

let type = ''
const service = axios.create({
	baseURL: '/api', // api 的 base_url
	timeout: 150000 // request timeout
})
// // request interceptor
service.interceptors.request.use(
  config => {
	  // 修改发送参数
	  config.headers['Content-Type'] = 'application/json;'
	  config.headers['Accept'] = 'application/json'
	  config.headers['type'] = 'web'
	  config.data = JSON.stringify(config.data)
	  return config
  },
  error => {
	  // Do something with request error
	  Promise.reject(error)
  }
)

// 请求返回
service.interceptors.response.use(
  response => {
	  const res = response.data
	  // 普通api请求返回
	  if (res.code === 0) {
		  return res
	  } else {
		  return { err: '错误' }
	  }
  },
  error => {
	  console.log('err' + error) // for debug
	  return Promise.reject(error)
  }
)

export default service
